package com.atguigui.leetcode;

import java.util.Arrays;

/**
 * 899.有序队列
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/8/3 9:41
 */
public class P899OrderlyQueue {

    public static void main(String[] args) {
        Solution solution = new P899OrderlyQueue().new Solution();
        // TO TEST
    }

    class Solution {
        public String orderlyQueue(String s, int k) {
            if (k == 1) {
                String smallest = s;
                StringBuilder sb = new StringBuilder(s);
                int n = s.length();

                for (int i = 1; i < n; i++) {
                    //拿到第一个字符，删除并添加到最后
                    char c = sb.charAt(0);
                    sb.deleteCharAt(0);
                    sb.append(c);

                    //对比操作后的字符串是否按照字典序变小了
                    if (sb.toString().compareTo(smallest) < 0) {
                        smallest = sb.toString();
                    }
                }
                return smallest;
            } else {
                char[] arr = s.toCharArray();
                Arrays.sort(arr);
                return new String(arr);
            }
        }
    }
}
